
package com.seekting.gongzhu.sdk.gson;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.seekting.gongzhu.sdk.exception.GsonException;
import com.seekting.gongzhu.sdk.gson.c2s.PreparedJsonC;
import com.seekting.gongzhu.sdk.gson.s2c.PlayerJsonS;
import com.seekting.gongzhu.sdk.gson.s2c.PrepareJsonS;

public class GsonConverter {
    private static final Gson GSON_TOOL = new Gson();

    private static final Map<String, Class<? extends AJson>> convertMap = new HashMap<String, Class<? extends AJson>>();

    static {
        convertMap.put(Card.class.getSimpleName(), Card.class);
        convertMap.put(PrepareJsonS.class.getSimpleName(), PrepareJsonS.class);
        convertMap.put(PreparedJsonC.class.getSimpleName(), PreparedJsonC.class);
        convertMap.put(PlayerJsonS.class.getSimpleName(), PlayerJsonS.class);
    }

    public static String convertStr(Object aJson) {
        if (aJson == null) {
            return null;
        }
        return GSON_TOOL.toJson(aJson);
    }

    public static AJson convertJson(String className, String jsonStr) {
        @SuppressWarnings("unchecked")
        Class<AJson> cl = (Class<AJson>) convertMap.get(className);
        if (cl == null) {
            throw new GsonException("can't find className=" + className + " gson in convertor");
        }
        return GSON_TOOL.fromJson(jsonStr, cl);

    }

    public static void main(String[] args) {

        // System.out.println(Card.class.getSimpleName());
        // Card card = new Card(CardType.Box, 1, 0);
        // // ActionGson.convertJson(Card.class, card);
        // String str = GsonConverter.convertStr(card);
        // System.out.println(str);
        // Card c = (Card) GsonConverter.convertJson(Card.class.getSimpleName(),
        // str);
        // System.out.println(c.getCardType());
        // System.out.println(c.getNumber());

    }
}
